import os
import src.utils.openpose.pyopenpose as op
from multiprocessing import Queue

# 获取当前文件所在目录的绝对路径
dir_path = os.path.dirname(os.path.abspath(__file__))
# 构建本地文件的绝对路径
file_path = os.path.abspath(os.path.join(dir_path, './openpose/models'))


def openpose_process(input_queue: Queue, output_queue: Queue) -> None:
    # Set up OpenPose
    params = {
        "model_folder": file_path,
        "net_resolution": "160x80",
        "model_pose": "BODY_25",
        "frame_step": 2,
        "process_real_time": False,
        "face_detector": 0,
        "disable_blending": False
    }

    opWrapper = op.WrapperPython()
    opWrapper.configure(params)
    opWrapper.start()

    while True:
        # Wait for input data from the main process
        input_data = input_queue.get()

        if input_data is None:
            # If received a None object, then stop the process
            break

        # Perform the image recognition using OpenPose
        image = input_data
        datum = op.Datum()
        datum.cvInputData = image
        opWrapper.emplaceAndPop([datum])

        # Get the results
        results = None
        if datum.poseKeypoints.ndim != 0:
            results = {
                "output_data": datum.cvOutputData,
                "key_points": datum.poseKeypoints[0]
            }

        # Put the results into the output queue to send back to the main process
        output_queue.put(results)

    # Clean up OpenPose and exit the process
    opWrapper.stop()
